package com.sdgakj.web.feed;

import com.sdgakj.entity.FeedTag;
import com.sdgakj.service.feed.FeedTagService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;

import javax.servlet.ServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/feedtag")
public class FeedTagController {

	private static final String PAGE_SIZE = "10";
    private static final String CREATE="create";
    private static final String UPDATE="update";
    private static final String RESULT_TRUE="true";
    private static final String RESULT_FALSE="false";
	
	@Autowired
	private FeedTagService feedTagService;
	
	/**
	 * 列表
	 * @param pageNumber 页号
	 * @param pageSize   每页大小
	 * @param sortType   排序字段
	 * @param model			
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sortType", defaultValue = "desc") String sortType, Model model,
			ServletRequest request) {
		Map<String,Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
		Page<FeedTag> feedtags = feedTagService.getFeedTagByPage(searchParams, pageNumber, pageSize, "seq", sortType);
		model.addAttribute("feedtags",feedtags);
		model.addAttribute("sortType", sortType);
		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
		return "feed/feedTagList";
	}
	
	/**
	 * 跳转添加页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="create",method=RequestMethod.GET)
	public String  create(Model model){
		model.addAttribute("action", "create");
		return "feed/feedTagForm";
	}
	
	@RequestMapping(value="create",method=RequestMethod.POST)
	public String createFeedTag(FeedTag entity,RedirectAttributes redirectAttributes){
//		entity.setCreateDate(new Date());
//		entity.setUpdateDate(new Date());
		entity.setDescp(StringUtils.replace(entity.getDescp(), " ", ""));
		feedTagService.saveFeedTag(entity);
		return "redirect:/feedtag/list";
	}
	
	/**
	 * 跳转修改页面
	 * @return
	 */
	@RequestMapping(value="update/{id}",method=RequestMethod.GET)
	public String update(@PathVariable("id") Long id, Model model){
		model.addAttribute("feedtag", feedTagService.getFeedTagById(id));
		model.addAttribute("action", "update");
		return "feed/feedTagForm";
	}
	
	@RequestMapping(value="update",method=RequestMethod.POST)
	public String updateInsurances( @ModelAttribute("model")FeedTag entity,RedirectAttributes redirectAttributes){
		entity.setUpdateDate(new Date());
		feedTagService.updateFeedTag(entity);
		redirectAttributes.addFlashAttribute("message", "更新成功");
		return "redirect:/feedtag/list";
	}
	
	
	@RequestMapping(value="/delete/{id}",method=RequestMethod.GET)
	public String delete(@PathVariable("id") Long id,RedirectAttributes redirectAttributes){
		feedTagService.deleteFeedTag(id);
		redirectAttributes.addFlashAttribute("message", "删除信息成功！");
		return "redirect:/feedtag/list";
	}
	
	@ModelAttribute
	public void getFeedTag(@RequestParam(value = "id", defaultValue = "-1") Long id, Model model) {
		if (id != -1) {
			model.addAttribute("model", feedTagService.getFeedTagById(id));
		}
	}
	
	/**
	 * 校验是否唯一
	 * @param type{create update}
	 */
	@RequestMapping(value = "/ajaxcheck")
	@ResponseBody
	public String checkName(@RequestParam("title") String title,@RequestParam(value = "op") String type,@RequestParam(value = "id", defaultValue = "0")Long id) {
		List<FeedTag>  list = null;
		if(StringUtils.isNotBlank(type) && StringUtils.isNotBlank(title)){
			list = feedTagService.getFeedTagByName(title);
		}else{
			return RESULT_FALSE;
		}
		if(list!=null && list.size()>0){
			if(type.equals(CREATE)){
				return "false";
			}else if(type.equals(UPDATE)){
				if(list.size()>1){
					return RESULT_FALSE;
				}else{
					if(list.get(0).getId().equals(id)){
						return RESULT_TRUE;
					}else{
						return RESULT_FALSE;
					}
				}
			}else{
				return RESULT_FALSE;
			}
		}else{
			return RESULT_TRUE;
		}
	}
	
}
